package cn.entergx.smallmengplan

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Toast
import cn.entergx.smallmengplan.network.Repo
import com.xuexiang.xui.widget.actionbar.TitleBar
import kotlinx.android.synthetic.main.activity_list.*
import kotlinx.android.synthetic.main.activity_list.titlebar
import kotlinx.android.synthetic.main.activity_write_post.*
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import java.lang.Exception

class WriteActivity : AppCompatActivity() {
    private lateinit var loveEvent: LoveEvent
    private var isSave:Boolean = true
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_write_post)
        try {
            loveEvent = intent.getSerializableExtra("LoveEvent")as LoveEvent
            isSave = false
            me_wp_title.contentText = loveEvent.content
            me_wp_content.contentText = loveEvent.note
            shine_button.isChecked = loveEvent.completed == 1
        }catch (e:Exception){
            loveEvent = LoveEvent()
            isSave = true
        }
        titlebar.setLeftClickListener {
            finish()
        }.addAction(object : TitleBar.TextAction("发送") {
            override fun performAction(view: View?) {
                if (isSave){
                    send()
                }else{
                    update()
                }
            }
        })
    }

    private fun update() {
        Repo.update(loveEvent.id,me_wp_title.contentText,
            me_wp_content.contentText,
            if (shine_button.isChecked) 1 else 0,
            object : Callback<Int> {
                override fun onResponse(call: Call<Int>, response: Response<Int>) {
                    if (response.body() == 0) {
                        Toast.makeText(this@WriteActivity, "更新成功", Toast.LENGTH_SHORT).show()
                    }else{
                        Toast.makeText(this@WriteActivity, "更新失败", Toast.LENGTH_SHORT).show()
                    }
                    setResult(0)
                    finish()
                }

                override fun onFailure(call: Call<Int>, t: Throwable) {
                    Toast.makeText(this@WriteActivity, "更新失败", Toast.LENGTH_SHORT).show()
                    setResult(1)
                    finish()
                }
            })
    }

    private fun send() {
        Repo.save(me_wp_title.contentText,
            me_wp_content.contentText,
            if (shine_button.isChecked) 1 else 0,
            object : Callback<Int> {
                override fun onResponse(call: Call<Int>, response: Response<Int>) {
                    if (response.body() == 0) {
                        Toast.makeText(this@WriteActivity, "发送成功", Toast.LENGTH_SHORT).show()
                    }else{
                        Toast.makeText(this@WriteActivity, "发送失败", Toast.LENGTH_SHORT).show()
                    }
                    finish()
                }

                override fun onFailure(call: Call<Int>, t: Throwable) {
                    Toast.makeText(this@WriteActivity, "发送失败", Toast.LENGTH_SHORT).show()
                    finish()
                }
            })
    }
}